package com.example.service;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.LeaveEnum;
import com.example.common.enums.LevelEnum;
import com.example.common.enums.ResultCodeEnum;
import com.example.common.enums.RoleEnum;
import com.example.entity.Account;
import com.example.entity.Employee;
import com.example.entity.Leave;
import com.example.exception.CustomException;
import com.example.mapper.EmployeeMapper;
import com.example.mapper.LeaveMapper;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 请假信息表业务处理
 **/
@Service
public class LeaveService {

    @Resource
    private LeaveMapper leaveMapper;
    @Resource
    private EmployeeMapper employeeMapper;

    /**
     * 新增
     */
    public void add(Leave leave) {
        // 先从JWT中获取当前用户
        Account currentUser = TokenUtils.getCurrentUser();
        // 根据当前用户的id，去employee查到完整的用户信息
        Employee employee = employeeMapper.selectById(currentUser.getId());
        // 将用户id、部门id信息塞给请假实体，使查询语句多表联查出用户表的姓名、部门表的部门名称，然后存放在实体类定义的关联属性
        leave.setEmployeeId(employee.getId());
        leave.setDepartmentId(employee.getDepartmentId());

        // 判断当前用户的等级level，如果是员工，设置审批进度为"待主管审批"
        if (LevelEnum.EMPLOYEE.level.equals(employee.getLevel())) {
            leave.setProcess(LeaveEnum.HEADER_APPLYING.process);
            // 如果是主管，设置审批进度为"待管理员审批"
        } else if (LevelEnum.HEADER.level.equals(employee.getLevel())) {
            leave.setProcess(LeaveEnum.ADMIN_APPLYING.process);
        }

        leaveMapper.insert(leave);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        leaveMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            leaveMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Leave leave) {
        String process = leave.getProcess();
        // 如果前台传过来的审批结果是空的，不能过，提示一下
        if (ObjectUtil.isEmpty(process)) {
            throw new CustomException(ResultCodeEnum.PARAM_PROCESS_ERROR);
        }
        // 获取当前登录的用户
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            Employee employee = employeeMapper.selectById(currentUser.getId());
            // 如果是主管审批
            if (LevelEnum.HEADER.level.equals(employee.getLevel())) {
                // 主管在前台传的"审批通过"，修改审批进度为"待管理员审批"；反之修改审批进度为"主管审批未通过"
                if (LeaveEnum.APPLY_OK.process.equals(process)) {
                    leave.setProcess(LeaveEnum.ADMIN_APPLYING.process);
                } else {
                    leave.setProcess(LeaveEnum.HEADER_APPLY_NO.process);
                }
            }
        }
        // 如果是管理员审批
        if (RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            // 管理员在前台传的"审批通过"，修改审批进度为"审批已通过"；反之修改审批进度为"管理员审批未通过"
            if (LeaveEnum.APPLY_OK.process.equals(process)) {
                leave.setProcess(LeaveEnum.APPLY_PASS.process);
            } else {
                leave.setProcess(LeaveEnum.ADMIN_APPLY_NO.process);
            }
        }
        leaveMapper.updateById(leave);
    }

    /**
     * 根据ID查询
     */
    public Leave selectById(Integer id) {
        return leaveMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Leave> selectAll(Leave leave) {
        return leaveMapper.selectAll(leave);
    }

    /**
     * 请假记录分页查询
     */
    // 用户(员工、主管)查看请假记录，只能看到自己的请假记录
    public PageInfo<Leave> selectPage(Leave leave, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            leave.setEmployeeId(currentUser.getId());
            // 管理员查看请假记录，只看到审批进度是"审批已通过"的所有数据，只看到有效请假记录，供财务部发放薪资作参考，对照扣薪资
        } else if (RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            leave.setProcess(LeaveEnum.APPLY_PASS.process);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Leave> list = leaveMapper.selectAll(leave);

        // set自定义属性放假状态status
        // 生成今天的日期，格式为年月日
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String today = format.format(new Date());

        // 比较日期大小
        for (Leave leave1:list) {
            if (today.compareTo(leave1.getStart()) < 0) {
                leave1.setStatus("未开始");
            } else if (today.compareTo(leave1.getEnd()) > 0) {
                leave1.setStatus("已结束");
            } else {
                leave1.setStatus("休假中");
            }
        }

        return PageInfo.of(list);
    }

    /**
     * 请假审批分页查询
     */
    public PageInfo<Leave> selectPage2(Leave leave, Integer pageNum, Integer pageSize) {
        Account currentUser = TokenUtils.getCurrentUser();
        // 先判断是用户，拿到用户信息
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            Employee employee = employeeMapper.selectById(currentUser.getId());
            // 又是主管
            if (LevelEnum.HEADER.level.equals(employee.getLevel())) {
                // 通过用户信息中的部门id查询指定部门(主管自己部门)的请假审批
                leave.setDepartmentId(employee.getDepartmentId());
                // 只显示"待主管审批"的数据
                leave.setProcess(LeaveEnum.HEADER_APPLYING.process);
            }
        }
        // 如果是管理员登录
        if (RoleEnum.ADMIN.name().equals(currentUser.getRole())) {
            // 判断请假实体的进度process是否是"待管理员审批"，只显示满足条件的数据
            leave.setProcess(LeaveEnum.ADMIN_APPLYING.process);
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Leave> list = leaveMapper.selectAll(leave);
        return PageInfo.of(list);
    }

}